package com.example.demo.service;

import com.example.demo.domain.CategoryDO;
import com.example.demo.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Scope("prototype")
public class QueryCategoryOperation implements CategoryOperation {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void doExecute(CategoryDO categoryDO) {
        List<CategoryDO> targetList = new ArrayList<>();

        List<CategoryDO> children = categoryMapper.listCategoryByParentId(categoryDO.getId());

        for(CategoryDO child:children){
            targetList.add(child);
            child.execute(this);
        }

        categoryDO.setChildren(targetList);

    }
}
